﻿using System;
using System.IO;
using System.Text;

namespace MayhemEventExeModule.LowLevel
{
	// Defines the data protocol for reading and writing strings on our stream
	public class StreamString
	{
		private Stream ioStream;
		private UnicodeEncoding streamEncoding;

		public StreamString(Stream ioStream)
		{
			this.ioStream = ioStream;
			streamEncoding = new UnicodeEncoding();
		}

		public string ReadString()
		{
			int len = 0;

			int b = ioStream.ReadByte();

			if (b == -1)
			{
				return null;
			}

			len = b * 256;
			len += ioStream.ReadByte();
			byte[] inBuffer = new byte[len];
			ioStream.Read(inBuffer, 0, len);

			return streamEncoding.GetString(inBuffer);
		}

		public int WriteString(string outString)
		{
			byte[] outBuffer = streamEncoding.GetBytes(outString);
			int len = outBuffer.Length;
			if (len > UInt16.MaxValue)
			{
				len = (int)UInt16.MaxValue;
			}

			ioStream.WriteByte((byte)(len / 256));
			ioStream.WriteByte((byte)(len & 255));
			ioStream.Write(outBuffer, 0, len);
			ioStream.Flush();

			return outBuffer.Length + 2;
		}
	}
}
